﻿#region Copyright
// Copyright (c) 2011-2012 RaisingForce Team (Alikin Sergey)
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Collections.ObjectModel;
using System.Diagnostics.Contracts;
using System.Windows.Input;
using RaisingForce.Raise.Presentation;
using RaisingForce.Raise.Presentation.ViewModel;

namespace RaisingForce.Raise.Presentation.ViewModel
{
    public class DialogViewModel :
        WindowViewModel
    {
        #region Constructors

        public DialogViewModel(
            ViewModelBase content,
            Action<DialogViewModel> okExecute = null,
            Func<DialogViewModel, bool> okCanExecute = null,
            Action<DialogViewModel> okExecutionFailed = null)
            : base(content)
        {
            Contract.Requires(content != null);
            this.okExecute = okExecute;
            this.okCanExecute = okCanExecute;
            this.okExecutionFailed = okExecutionFailed;
            this.okCommand = new Lazy<ICommand>(
                () => new DelegateCommand(
                    this.OkCommandExecute,
                    this.OkCommandCanExecute));
            this.cancelCommand = new Lazy<ICommand>(
                () => new DelegateCommand(
                    this.CancelCommandExecute));
        }

        #endregion

        #region Private Declarations

        private readonly Lazy<ICommand> okCommand;

        private readonly Lazy<ICommand> cancelCommand;

        private readonly Action<DialogViewModel> okExecute;

        private readonly Action<DialogViewModel> okExecutionFailed;

        private readonly Func<DialogViewModel, bool> okCanExecute;

        private bool OkCanFailMode
        {
            get { return this.okExecutionFailed != null; }
        }

        private bool OkCanExecute()
        {
            return this.okCanExecute != null ? this.okCanExecute(this) : true;
        }

        private void OkExecute()
        {
            if (this.okExecute != null)
            {
                this.okExecute(this);
            }
        }

        private void OkCommandExecute(object param)
        {
            if (!this.OkCanFailMode
                || this.OkCanFailMode && this.OkCanExecute())
            {
                this.OkExecute();
                this.DialogResult = true;
                this.CloseCommand.Execute(null);
            }
            else
            {
                this.okExecutionFailed(this);
            }
        }

        private bool OkCommandCanExecute(object param)
        {
            return this.OkCanFailMode ? true : this.OkCanExecute();
        }

        private void CancelCommandExecute(object param)
        {
            this.DialogResult = false;
            this.CloseCommand.Execute(null);
        }

        #endregion

        #region ViewModel Properties

        public ICommand OkCommand
        {
            get { return this.okCommand.Value; }
        }

        public ICommand CancelCommand
        {
            get { return this.cancelCommand.Value; }
        }

        private ObservableCollection<CommandViewModel> commands;

        private const string propCommands = "Commands";

        public ObservableCollection<CommandViewModel> Commands
        {
            get
            {
                Contract.Ensures(Contract.Result<ObservableCollection<CommandViewModel>>() != null);
                return this.GetNotNullProperty(ref this.commands, propCommands);
            }
            set
            {
                this.SetProperty(ref this.commands, value, propCommands);
            }
        }

        #endregion
    }
}
